<template>
  <div>
    <div style="position: relative;">
      <h3>办训办学评估</h3>
      <vxe-input style="position: absolute;top: 10px;right: 10px;" v-model.trim.lazy="filterName" type="search"
        placeholder="快捷检索" @keyup="searchEvent"></vxe-input>
      <div class="container">
        <vxe-table resizable ref="XTable" show-overflow border max-height="800" stripe highlight-hover-row
          highlight-current-row :loading="loading" :data="allData4centerData" @sort-change="sortChange">
          <vxe-table-column field="orgName" title="机构" sortable></vxe-table-column>
          <vxe-table-column field="selfAssessScore" title="学校自评" sortable></vxe-table-column>
          <vxe-table-column field="reAssessScore" title="市局初审" sortable></vxe-table-column>
          <vxe-table-column field="finalAssessScore" title="省局复核" sortable></vxe-table-column>
          <vxe-table-column field="check" title="操作" width="300" align="center" sortable>
            <template #default="{ row }">
              <div style="display: flex;justify-content: space-around;">
                <el-link @click="editModalShow(row)" type="primary">评分表</el-link>
                <!-- <el-link @click="fileDownLoad(row)" type="primary" style="margin-left: 20px;">自评报告下载 <i class="el-icon-download"></i> </el-link> -->
                <el-dropdown size="" split-button type="primary" @click="fileDownLoadOne(row)" @command="downloadFile">
                  自评报告下载
                  <el-dropdown-menu size="" slot="dropdown">
                    <el-dropdown-item size="" v-for="item,index in row['fileList']" :command="item">{{item}}下载
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </div>
            </template>
          </vxe-table-column>
        </vxe-table>
        <vxe-pager :loading="loading" :current-page="page.currentPage" :page-size="page.pageSize"
          :total="page.totalResult" :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
          @page-change="handlePageChange">
        </vxe-pager>
      </div>
    </div>
    <div style="position: relative;margin-top: 20px;">
      <h3>办训办学评估汇总</h3>
      <div style="position: absolute;top: 10px;right: 10px;">
        <el-button @click="exportEvent" size="small" type="primary" icon="el-icon-download">导出</el-button>
        <el-button @click="printEvent" size="small" type="primary" icon="el-icon-printer">打印</el-button>
      </div>
      <div class="container">
        <vxe-table resizable ref="XTable2" show-overflow border max-height="800" stripe highlight-hover-row
          highlight-current-row :loading="loading2" :data="allData4centerStatisticsData" @sort-change="sortChange2">
          <vxe-table-column field="orgName" title="机构" sortable></vxe-table-column>
          <vxe-table-column field="1" title="基本条件" sortable></vxe-table-column>
          <vxe-table-column field="8" title="科研医务服务保障" sortable></vxe-table-column>
          <vxe-table-column field="3" title="训练过程" sortable></vxe-table-column>
          <vxe-table-column field="4" title="教育教学" sortable></vxe-table-column>
          <vxe-table-column field="5" title="人才质量" sortable></vxe-table-column>
          <vxe-table-column field="6" title="人才效益" sortable></vxe-table-column>
          <vxe-table-column field="9" title="才效益追加" sortable></vxe-table-column>
          <vxe-table-column field="14" title="办训规模" sortable></vxe-table-column>
          <vxe-table-column field="7" title="总分" sortable></vxe-table-column>
          <vxe-table-column field="reAssessScore" title="市局初审" sortable></vxe-table-column>
          <vxe-table-column field="finalAssessScore" title="省局复核" sortable></vxe-table-column>
        </vxe-table>
        <vxe-pager :loading="loading2" :current-page="page2.currentPage" :page-size="page2.pageSize"
          :total="page2.totalResult" :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
          @page-change="handlePageChange2">
        </vxe-pager>
      </div>
    </div>
    <vxe-modal v-model="editModal" :title="currentRow.orgName" height="800" show-zoom resize width="1000"
      :lockView="false">
      <template #default>
        <centerTable :dataList="dataList" :footerArr="footerArr" @saveSuccess="saveSuccess" :currentRow="currentRow" />
      </template>
    </vxe-modal>
  </div>
</template>

<script>
  import { allData4center, allData4centerStatistics, testFileDownload, allData4org } from '@/api/statistics.js'
  import { listFiles } from '@/api/organization.js'
  import centerTable from '@/components/province/centerTable.vue'
  import { handleBatchDownload } from '@/utils/packdownload'
  import Decimal from "decimal.js"
  export default {
    components: { centerTable },
    data() {
      return {
        allData4centerDefaultData: [],
        allData4centerData: [],
        page: {
          currentPage: 1,
          pageSize: 10,
          totalResult: 0
        },
        loading: false,
        filterName: '',
        searchProps: [],
        allData4centerStatisticsDefaultData: [],
        allData4centerStatisticsData: [],
        page2: {
          currentPage: 1,
          pageSize: 10,
          totalResult: 0
        },
        loading2: false,
        filterName2: '',
        searchProps2: [],
        editModal: false,
        editData: [],
        dataList: [],
        footerArr: [],
        currentRow: {},
        fileList: []
      }
    },
    created() {
      this.getAllData4center()
      this.getAllData4centerStatistics()
    },
    methods: {
      saveSuccess() {
        this.getAllData4center()
        setTimeout(() => {
          this.editModalShow(this.currentRow)
        }, 500)
      },
      editModalShow(row) {
        this.currentRow = row
        let data = {
          orgId: row.selfAssessOrgId,
          version: '2023'
        }
        allData4org(data).then(res => {
          let iteam1 = 0
          let iteam2 = 0
          let iteam3 = 0
          let iteam4 = 0
          let city1 = 0
          let city2 = 0
          let city3 = 0
          let city4 = 0
          let province1 = 0
          let province2 = 0
          let province3 = 0
          let province4 = 0

          let outiteam1 = 0
          let outiteam2 = 0
          let outiteam3 = 0
          let outiteam4 = 0
          let outcity1 = 0
          let outcity2 = 0
          let outcity3 = 0
          let outcity4 = 0
          let outprovince1 = 0
          let outprovince2 = 0
          let outprovince3 = 0
          let outprovince4 = 0
          if (res.code === 200) {
            res.data.forEach((item) => {
              //转换数据  指标得分 -----自评分数
              item["reAssessScore"] = item["reAssessScore"] ? item["reAssessScore"] : 0;
              item["finalAssessScore"] = item["finalAssessScore"] ? item["finalAssessScore"] : 0;
              item["selfAssessScore"] = item["selfAssessScore"] ? item["selfAssessScore"] : 0;
              item["titleWeightWithoutCulture"] = item["titleWeightWithoutCulture"] ? item[
                "titleWeightWithoutCulture"] : 0;
              item["subTitleWeightCulture"] = item["subTitleWeightCulture"] ? item["subTitleWeightCulture"] : 0;
              item["titleWeight"] = item["titleWeight"] ? item["titleWeight"] : 0;
              item["subTitleWeight"] = item["subTitleWeight"] ? item["subTitleWeight"] : 0;
              if (item["reAssessMemo"] == "依附普通中小学进行文化教育") {

                if (item["titleName"] == "基本条件") {
                  outiteam1 += Number(Decimal(item["selfAssessScore"]).mul(Decimal(item[
                    "titleWeightWithoutCulture"])).mul(
                    Decimal(item[
                      "subTitleWeightCulture"])) * 1000)

                  //市局 -------------总分
                  outcity1 += Number(Decimal(item["reAssessScore"]).mul(Decimal(item[
                    "titleWeightWithoutCulture"])).mul(
                    Decimal(item[
                      "subTitleWeightCulture"])) * 1000)

                  //省局--------------总分

                  outprovince1 += Number(Decimal(item["finalAssessScore"]).mul(Decimal(item[
                    "titleWeightWithoutCulture"])).mul(
                    Decimal(item[
                      "subTitleWeightCulture"])) * 1000)


                }
                if (item["titleName"] == "科研医务") {
                  outiteam2 += Number(Decimal(item["selfAssessScore"]).mul(Decimal(item[
                    "titleWeightWithoutCulture"])).mul(
                    Decimal(item[
                      "subTitleWeightCulture"])) * 1000)
                  //市局 -------------总分
                  outcity2 += Number(Decimal(item["reAssessScore"]).mul(Decimal(item[
                    "titleWeightWithoutCulture"])).mul(
                    Decimal(item[
                      "subTitleWeightCulture"])) * 1000)
                  //省局--------------总分

                  outprovince2 += Number(Decimal(item["finalAssessScore"]).mul(Decimal(item[
                    "titleWeightWithoutCulture"])).mul(
                    Decimal(item[
                      "subTitleWeightCulture"])) * 1000)

                }
                if (item["titleName"] == "训练过程") {
                  outiteam3 += Number(Decimal(item["selfAssessScore"]).mul(Decimal(item[
                    "titleWeightWithoutCulture"])).mul(
                    Decimal(item[
                      "subTitleWeightCulture"])) * 1000)
                  //市局 -------------总分
                  outcity3 += Number(Decimal(item["reAssessScore"]).mul(Decimal(item[
                    "titleWeightWithoutCulture"])).mul(
                    Decimal(item[
                      "subTitleWeightCulture"])) * 1000)
                  //省局--------------总分

                  outprovince3 += Number(Decimal(item["finalAssessScore"]).mul(Decimal(item[
                    "titleWeightWithoutCulture"])).mul(
                    Decimal(item[
                      "subTitleWeightCulture"])) * 1000)

                }
                if (item["titleName"] == "教育教学") {
                  outiteam4 += Number(Decimal(item["selfAssessScore"]).mul(Decimal(item[
                    "titleWeightWithoutCulture"])).mul(
                    Decimal(item[
                      "subTitleWeightCulture"])) * 1000)
                  //市局 -------------总分
                  outcity4 += Number(Decimal(item["reAssessScore"]).mul(Decimal(item[
                    "titleWeightWithoutCulture"])).mul(
                    Decimal(item[
                      "subTitleWeightCulture"])) * 1000)
                  //省局--------------总分

                  outprovince4 += Number(Decimal(item["finalAssessScore"]).mul(Decimal(item[
                    "titleWeightWithoutCulture"])).mul(
                    Decimal(item[
                      "subTitleWeightCulture"])) * 1000)

                }


              } else {
                if (item["titleName"] == "基本条件") {
                  iteam1 += Number(Decimal(item["selfAssessScore"]).mul(Decimal(item["titleWeight"])).mul(
                    Decimal(item[
                      "subTitleWeight"])) * 1000)

                  //市局 -------------总分
                  city1 += Number(Decimal(item["reAssessScore"]).mul(Decimal(item["titleWeight"])).mul(
                    Decimal(item[
                      "subTitleWeight"])) * 1000)

                  //省局--------------总分

                  province1 += Number(Decimal(item["finalAssessScore"]).mul(Decimal(item["titleWeight"])).mul(
                    Decimal(item[
                      "subTitleWeight"])) * 1000)


                }
                if (item["titleName"] == "科研医务") {
                  iteam2 += Number(Decimal(item["selfAssessScore"]).mul(Decimal(item["titleWeight"])).mul(
                    Decimal(item[
                      "subTitleWeight"])) * 1000)
                  //市局 -------------总分
                  city2 += Number(Decimal(item["reAssessScore"]).mul(Decimal(item["titleWeight"])).mul(
                    Decimal(item[
                      "subTitleWeight"])) * 1000)
                  //省局--------------总分

                  province2 += Number(Decimal(item["finalAssessScore"]).mul(Decimal(item["titleWeight"])).mul(
                    Decimal(item[
                      "subTitleWeight"])) * 1000)

                }
                if (item["titleName"] == "训练过程") {
                  iteam3 += Number(Decimal(item["selfAssessScore"]).mul(Decimal(item["titleWeight"])).mul(
                    Decimal(item[
                      "subTitleWeight"])) * 1000)
                  //市局 -------------总分
                  city3 += Number(Decimal(item["reAssessScore"]).mul(Decimal(item["titleWeight"])).mul(
                    Decimal(item[
                      "subTitleWeight"])) * 1000)
                  //省局--------------总分

                  province3 += Number(Decimal(item["finalAssessScore"]).mul(Decimal(item["titleWeight"])).mul(
                    Decimal(item[
                      "subTitleWeight"])) * 1000)

                }
                if (item["titleName"] == "教育教学") {
                  iteam4 += Number(Decimal(item["selfAssessScore"]).mul(Decimal(item["titleWeight"])).mul(
                    Decimal(item[
                      "subTitleWeight"])) * 1000)
                  //市局 -------------总分
                  city4 += Number(Decimal(item["reAssessScore"]).mul(Decimal(item["titleWeight"])).mul(
                    Decimal(item[
                      "subTitleWeight"])) * 1000)
                  //省局--------------总分

                  province4 += Number(Decimal(item["finalAssessScore"]).mul(Decimal(item["titleWeight"])).mul(
                    Decimal(item[
                      "subTitleWeight"])) * 1000)

                }
              }




            })





            if (res.data[6].reAssessMemo == "依附普通中小学进行文化教育") {
              //基本条件总分
              res.data[6].selfAssessScore = outiteam1 * 0.001
              res.data[6].reAssessScore = outcity1 * 0.001
              res.data[6].finalAssessScore = outprovince1 * 0.001

              //科研医务
              res.data[10].selfAssessScore = outiteam2 * 0.001
              res.data[10].reAssessScore = outcity2 * 0.001
              res.data[10].finalAssessScore = outprovince2 * 0.001
              //训练过程
              res.data[16].selfAssessScore = outiteam3 * 0.001
              res.data[16].reAssessScore = outcity3 * 0.001
              res.data[16].finalAssessScore = outprovince3 * 0.001
              //教育教学
              res.data[23].selfAssessScore = outiteam4 * 0.001
              res.data[23].reAssessScore = outcity4 * 0.001
              res.data[23].finalAssessScore = outprovince4 * 0.001

            } else {
              //基本条件总分
              res.data[6].selfAssessScore = iteam1 * 0.001
              res.data[6].reAssessScore = city1 * 0.001
              res.data[6].finalAssessScore = province1 * 0.001

              //科研医务
              res.data[10].selfAssessScore = iteam2 * 0.001
              res.data[10].reAssessScore = city2 * 0.001
              res.data[10].finalAssessScore = province2 * 0.001
              //训练过程
              res.data[16].selfAssessScore = iteam3 * 0.001
              res.data[16].reAssessScore = city3 * 0.001
              res.data[16].finalAssessScore = province3 * 0.001
              //教育教学
              res.data[23].selfAssessScore = iteam4 * 0.001
              res.data[23].reAssessScore = city4 * 0.001
              res.data[23].finalAssessScore = province4 * 0.001
            }

            this.editData = res.data


            this.dataList = this.editData
            this.footerArr = this.dataList.pop()
          } else {
            this.editData = []
          }
        })
        this.editModal = true
      },
      downloadFile(item) {
        let name = item.substring(0, item.lastIndexOf("."))
        let suffix = item.match(/\.([^\.]+)$/)[1]

        window.open(this.$http + '/base/fileDownloadOSS/' + this.currentRow.selfAssessOrgId + '/' + name + '/' + suffix)
      },
      fileDownLoadOne(row) {
        this.currentRow = row
        listFiles({ orgId: row.selfAssessOrgId }).then(res => {
          if (res.code === 200 && res.data != 0) {
            row.fileList = res.data
            this.$forceUpdate()
            // console.log(1114);
          } else {
            row.fileList = []
            this.$message.warning('没有文件！')
          }
        })
      },
      fileDownLoad(row) {
        let data = []
        listFiles({ orgId: row.selfAssessOrgId }).then(res => {
          if (res.code === 200 && res.data != 0) {
            this.fileList = res.data
            res.data.forEach(item => {
              let name = item.substring(0, item.lastIndexOf("."))
              let suffix = item.match(/\.([^\.]+)$/)[1]

              let url = this.$http + '/base/fileDownloadOSS/' + row.selfAssessOrgId + '/' + name + '/' + suffix
              handleBatchDownload(url)
              // data.push(this.$http+'/base/fileDownloadOSS/'+10+'/'+name+'/'+suffix)
              // window.open(this.$http+'/base/fileDownloadOSS/'+10+'/'+name+'/'+suffix)
            })
            // console.log(data)
            // handleBatchDownload(data)
          } else {
            this.fileList = []
            // data = []
            this.$message.warning('没有文件！')
          }
        })
        // testFileDownload(row.selfAssessOrgId).then(res=>{
        //   if(res.code === 200) {
        //     window.open(process.env.VUE_APP_BASE_API + '/base/fileDownload/' + row.selfAssessOrgId)
        //   }else{
        //     this.$message.warning('没有文件！')
        //   }
        // })
      },
      getAllData4center() {
        allData4center().then(res => {
          if (res.code === 200 && res.data.length != 0) {
            this.searchProps = Object.keys(res.data[0])
            res.data.fileList = []
            this.allData4centerDefaultData = res.data
            // console.log(this.allData4centerDefaultData, "555555555555");
          } else {
            this.allData4centerDefaultData = []
          }
        }).then(() => {
          this.findList()
        })
      },
      searchEvent() {
        const filterName = this.$XEUtils.toValueString(this.filterName).trim().toLowerCase()
        if (filterName) {
          const filterRE = new RegExp(filterName, 'gi')
          const searchProps = this.searchProps
          const rest = this.allData4centerDefaultData.filter(item => searchProps.some(key => this.$XEUtils
            .toValueString(item[
              key]).toLowerCase().indexOf(filterName) > -1))
          this.allData4centerData = rest.map(row => {
            const item = Object.assign({}, row)
            searchProps.forEach(key => {
              item[key] = this.$XEUtils.toValueString(item[key]).replace(filterRE, match => match)
            })
            return item
          })
        } else {
          this.allData4centerData = this.allData4centerDefaultData
          this.findList()
        }
      },
      handlePageChange({
        currentPage,
        pageSize
      }) {
        this.page.currentPage = currentPage
        this.page.pageSize = pageSize
        this.findList()
      },
      sortChange() {
        this.page.pageSize = this.allData4centerDefaultData.length
        this.findList()
      },
      findList() {
        this.loading = true
        setTimeout(() => {
          this.loading = false
          this.page.totalResult = this.allData4centerDefaultData.length
          this.allData4centerData = this.allData4centerDefaultData.slice((this.page.currentPage - 1) * this.page
            .pageSize, this.page.currentPage * this.page.pageSize)
        }, 300)
      },
      exportEvent() {
        this.loading2 = true
        setTimeout(() => {
          this.$refs.XTable2.exportData({
            filename: '办训办学评估汇总',
            isHeader: true,
            isFooter: true,
            data: this.allData4centerStatisticsData
          })
          this.loading2 = false
        }, 100)
      },
      printEvent() {
        let topHtml = '<h2>办训办学评估汇总</h2>'
        let num = this.page2.pageSize
        this.page2.pageSize = this.allData4centerDefaultData.length
        this.findList2()
        setTimeout(() => {
          this.$refs.XTable2.print({
            sheetName: '办训办学评估汇总',
            beforePrintMethod: ({ content }) => {
              return topHtml + content
            }
          })
          this.page2.pageSize = num
          this.findList2()
        }, 1000)
      },
      getAllData4centerStatistics() {
        allData4centerStatistics().then(res => {
          if (res.code === 200 && res.data.length != 0) {
            this.searchProps2 = Object.keys(res.data[0])
            // console.log(res.data)
            res.data.forEach((item) => {
              item[1] = item[1] ? Number(item[1]) : 0;
              item[8] = item[8] ? Number(item[8]) : 0;
              item[3] = item[3] ? Number(item[3]) : 0;
              item[4] = item[4] ? Number(item[4]) : 0;
              item[5] = item[5] ? Number(item[5]) : 0;
              item[6] = item[6] ? Number(item[6]) : 0;
              item[7] = item[7] ? Number(item[7]) : 0;
              item[9] = item[9] ? Number(item[9]) : 0;
              item[14] = item[14] ? Number(item[14]) : 0;
              item[7] =
                Decimal(item[1]).add(Decimal(item[8])).add(Decimal(item[3])).add(Decimal(item[4])).add(Decimal(
                  item[5])).add(Decimal(item[6]))
                .add(Decimal(item[9])).add(Decimal(item[14]))

              // console.log(Number(item[7]));
            })
            this.allData4centerStatisticsDefaultData = res.data

          } else {
            this.allData4centerStatisticsDefaultData = []
          }
        }).then(() => {
          this.findList2()
        })
      },
      searchEvent2() {
        const filterName = this.$XEUtils.toValueString(this.filterName2).trim().toLowerCase()
        if (filterName) {
          const filterRE = new RegExp(filterName, 'gi')
          const searchProps = this.searchProps2
          const rest = this.allData4centerStatisticsDefaultData.filter(item => searchProps.some(key => this.$XEUtils
            .toValueString(item[
              key]).toLowerCase().indexOf(filterName) > -1))
          this.allData4centerStatisticsData = rest.map(row => {
            const item = Object.assign({}, row)
            searchProps.forEach(key => {
              item[key] = this.$XEUtils.toValueString(item[key]).replace(filterRE, match => match)
            })
            // console.log(item, "6666666666");
            return item
          })
        } else {
          this.allData4centerStatisticsData = this.allData4centerStatisticsDefaultData
          this.findList2()
        }
      },
      handlePageChange2({
        currentPage,
        pageSize
      }) {
        this.page2.currentPage = currentPage
        this.page2.pageSize = pageSize
        this.findList2()
      },
      sortChange2() {
        this.page2.pageSize = this.allData4centerStatisticsDefaultData.length
        this.findList2()
      },
      findList2() {
        this.loading2 = true
        setTimeout(() => {
          this.loading2 = false
          this.page2.totalResult = this.allData4centerStatisticsDefaultData.length
          this.allData4centerStatisticsData = this.allData4centerStatisticsDefaultData.slice((this.page2
              .currentPage - 1) * this.page2
            .pageSize, this.page2.currentPage * this.page2.pageSize)
        }, 300)
      },
    }
  }
</script>

<style scoped>
  .container {
    padding: 10px;
    background-color: #FFFFFF;
  }
</style>
